home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / bastips2.zip / BASFRACT.TXT next >
Text File  |  1986-07-02  |  10KB  |  247 lines

  1.                       IBM Fractal Graphics
  2.         (COMPUTE! Magazine March 1986 by Paul W. Carlson)
  3.  
  4.      The term fractal was coined by Benoit Mandelbrot to denote curves
  5. or surfaces having fractional dimension.  The concept of fractional
  6. dimension can be illustrated as follows: A straight curve (a line) is
  7. one-dimensional, having only length.  However, if the curve is
  8. infinitely long and curves about in such a manner as to completely
  9. fill an area of the plane containing it, the curve could be considered
  10. two-dimensional.  A curve partially filling an area would have a
  11. fractional dimension between one and two.
  12.  
  13.      Many types of fractals are self-similar, which means that all
  14. portions of the fractal resemble each other.  Self-similarity occurs
  15. whenever the whole is an expansion of some basic building block.  In
  16. the language of fractals, this basic building block is called the
  17. generator.  The generator in the accompanying programs consists of a
  18. number of connected line segments.  The curves that the programs plot
  19. are the result of starting with the generator and then repeatedly
  20. replacing each line segment with the whole generator according to a
  21. defined rule.  Theoretically, these replacement cycles would continue
  22. indefinitely.  In practice, the screen resolution limits the number
  23. of cycles.
  24.  
  25.      The programs illustrate two types of fractal curves.  The curves
  26. generated by Programs 1 and 2 are self-contacting, while the curve
  27. generated by Program 3 is self-avoiding.  A self-contacting curve
  28. touches itself but does not cross itself.  A self-avoiding curve never
  29. actually trouches itself although it may appear to because of the
  30. limited screen resolution.
  31.  
  32.      Program 1 plots a "dragon sweep."  It demonstrates in a step-by-
  33. step fashion how a fractal curve is filled.  The generator consists of
  34. two line segments of equal length forming a right angle.  During each
  35. replacement cycle, the generator is substituted for each segment on
  36. alternating sides of the segments, that is, to the left of the first
  37. segment, to the right of the second segment, and so on.  This BASIC
  38. program is slow enough to let you observe the development of the curve.
  39.  
  40.      The program prompts you to enter an even number of cycles (for
  41. reasons of efficiency and screen resolution, only even numbers of
  42. cycles are plotted).  When a plot is complete, pressing any key clears
  43. the screen and returns you to the prompt.  Start with two cycles, then
  44. four, etc.  It takes 14 cycles to completely fill in the "dragon," but
  45. since this requires almost two hours, you will probably want to quit
  46. after about 10 cycles.  You can see the complete dragon by running
  47. Program 2, which always plots the dragon first in less than 30 seconds.
  48.  
  49.      Since it's not at all obvious how the program works, here's a
  50. brief explanation.  NC is the number ov cycles; C is the cycle number;
  51. SN is an array of segment numbers indexed by cycle number; L is the
  52. segment length; D is the segment direction, numbered clockwise from
  53. the positive x direction; and X and Y are the high-resolution screen
  54. coordinates.
  55.  
  56. Lines 100-140     Get number of cycles from user
  57.  
  58. Line 150          Computes segment length
  59.  
  60. Line 160          Sets starting coordinates
  61.  
  62. Line 170          Sets segment numbers for all cycles to the first
  63.                   segment
  64.  
  65. Lines 180-220     Find the direction of the segment in the last cycle
  66.                   by rotating the segment in each cycle that will
  67.                   contain the segment in the last cycle
  68.  
  69. Lines 230-260     Increase or decrease X or Y by the segment length,
  70.                   depending on the segment direction
  71.  
  72. Lines 270-290     Plot the segment and update the current segment
  73.                   number for each cycle
  74.  
  75. Lines 300-320     If the segment number for cycle zero is still zero,
  76.                   do the next segment; otherwise, we're done
  77.  
  78.      Program 2 plots more than 8000 different dragons.  It does this
  79. by randomly determining on which side of the first segment the generator
  80. will be substituted for all cycles after the first cycle.  The generator
  81. is always substituted to the left of the first segment in the first
  82. cycle to avoid plotting off the screen.  Other than the randomization,
  83. this programs uses the same logic as Program 1.  The main part of this
  84. program is written in machine language to reduce the time required to
  85. plot a completely filled-in dragon from about two hours to less than
  86. half a minute.
  87.  
  88.      All the dragons are plotted after 14 cycles of substitution.  All
  89. have exactly the same area, which equals half of the square of the
  90. distance between the first and last points plotted.  All the dragons
  91. begin and end at the same points.
  92.  
  93.      When a plot is complete, press the space bar to plot another
  94. dragon, or press Q to quit.
  95.  
  96.      Program 3 plots a "snowflake sweep."  The segments are numbered
  97. zero through six, starting at the right.  The program is basically the
  98. same as Program 1.  The variables NC, C, SN, D, X, and Y represent the
  99. same values except that the direction D is numbered counterclockwise
  100. from the negative x direction.
  101.  
  102.  
  103. Line 20         Reads values of SD and RD.  Compute LN values.
  104.  
  105. Lines 30-50     Compute delta x and delta y factors for each direction
  106.  
  107. Lines 60-100    Get number of cycles from user
  108.  
  109. Line 120        Sets starting coordinates
  110.  
  111.  
  112. Line 130        Sets the segment numbers for all cycles to the first
  113.                 segment
  114.  
  115. Lines 140-170   Find the direction of the segment in the last cycle
  116.  
  117. Lines 180-190   Compute the coordinates of the end of the segment,
  118.                 plot the segment, and update the segment numbers for
  119.                 each cycle
  120.  
  121. Lines 200-220   Same as lines 300-320 in Program 1
  122.  
  123.  
  124.      Like Program 1, pressing any key when a plot is complete clears
  125. the screen and brings another prompt.
  126.  
  127.      Don't be afraid to experiment -- try modifying the shape of the
  128. generator in Program 3, for example.
  129.  
  130.  
  131. Program 1:  The Dragon Sweep
  132.  
  133. 90 DIM SN(14):KEY OFF
  134. 100 CLS:SCREEN 0
  135. 110 PRINT"ENTER AN EVEN NO. OF CYCLES (2 TO 14)"
  136. 120 INPUT"          OR ENTER A ZERO TO QUIT: ";NC
  137. 130 IF NC=0 THEN KEY ON:END
  138. 140 IF NC MOD 2=1 OR NC<2 OR NC>14 THEN 100
  139. 150 L=128:FOR C=2 TO NC STEP 2:L=L/2:NEXT
  140. 160 X=192:Y=133:CLS:SCREEN 2:PSET (X,Y),1
  141. 170 FOR C=0 TO NC:SN(C)=0:NEXT
  142. 180 D=0:FOR C=1 TO NC:IF SN(C-1)=SN(C) THEN D=D-1:GOTO 200
  143. 190 D=D+1
  144. 200 IF D=-1 THEN D=7
  145. 210 IF D=8 THEN D=0
  146. 220 NEXT
  147. 230 IF D=0 THEN X=X+L+L:GOTO 270
  148. 240 IF D=2 THEN Y=Y+L:GOTO 270
  149. 250 IF D=4 THEN X=X-L-L:GOTO 270
  150. 260 Y=Y-L
  151. 270 LINE -(X,Y),1:SN(NC)=SN(NC)+1
  152. 280 FOR C=NC TO 1 STEP -1:IF SN(C)<>2 THEN 300
  153. 290 SN(C)=0:SN(C-1)=SN(C-1)+1:NEXT
  154. 300 IF SN(0)=0 THEN 180
  155. 310 IF INKEY$="" THEN 310
  156. 320 GOTO 100
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. Program 2: Eight Thousand Dragons
  168.  
  169. 100 DEF SEG:CLEAR,&H3FF0:N=&H4000
  170. 110 READ A$:IF A$="/" THEN 130
  171. 120 POKE N,VAL("&H"+A$):N=N+1:GOTO 110
  172. 130 N=&H440F:FOR K=1 TO 15:POKE N,0:N=N+1:NEXT
  173. 140 POKE &H4425,0
  174. 150 N=&H4000:CALL N:POKE &H4425,1
  175. 160 A$=INKEY$:IF A$="" THEN 160
  176. 170 IF A$=" " THEN 150
  177. 180 IF A$<>"Q" AND A$<>"q" THEN 160
  178. 190 SCREEN 0:CLS:KEY ON:END
  179. 1000 DATA 1E,0E,1F,B8,05,00,CD,10,80,3E
  180. 1010 DATA 25,44,00,75,0B,B4,00,CD,1A,89
  181. 1020 DATA 16,23,44,EB,31,90,BE,02,00,B9
  182. 1030 DATA 08,00,A1,23,44,33,D2,A9,02,00
  183. 1040 DATA 74,02,B2,01,A9,04,00,74,02,B6
  184. 1050 DATA 01,32,D6,D0,EA,D1,D8,E2,E8,A3
  185. 1060 DATA 23,44,24,01,88,84,0F,44,46,83
  186. 1070 DATA FE,0F,75,D3,B8,00,06,33,C9,BA
  187. 1080 DATA 4F,18,32,FF,CD,10,B9,0F,00,33
  188. 1090 DATA F6,C6,84,00,44,00,46,E2,F8,C7
  189. 1100 DATA 06,1E,44,60,00,C7,06,20,44,84
  190. 1110 DATA 00,B8,01,0C,8B,0E,1E,44,8B,16
  191. 1120 DATA 20,44,CD,10,C6,06,22,44,00,B9
  192. 1130 DATA 0E,00,33,FF,BE,01,00,8A,A5,0F
  193. 1140 DATA 44,80,FC,00,75,18,FE,06,22,44
  194. 1150 DATA 8A,85,00,44,3A,84,00,44,75,20
  195. 1160 DATA FE,0E,22,44,FE,0E,22,44,EB,16
  196. 1170 DATA FE,03,22,44,8A,85,00,44,3A,84
  197. 1180 DATA 00,44,75,08,FE,06,22,44,FE,06
  198. 1190 DATA 22,44,80,3E,22,44,FF,75,07,C6
  199. 1200 DATA 06,22,44,07,EB,0C,80,3E,22,44
  200. 1210 DATA 08,75,05,C6,06,22,44,00,47,46
  201. 1220 DATA E2,AB,EB,02,EB,9A,80,3E,22,44
  202. 1230 DATA 00,75,06,FF,06,1E,44,EB,1E,80
  203. 1240 DATA 3E,22,44,02,75,06,FF,06,20,44
  204. 1250 DATA EB,11,80,3E,22,44,04,75,06,FF
  205. 1260 DATA 0E,1E,44,EB,04,FF,0E,20,44,B8
  206. 1270 DATA 01,0C,8B,0E,1E,44,8B,16,20,44
  207. 1280 DATA CD,10,FE,06,0E,44,BF,0D,00,BE
  208. 1290 DATA 0E,00,B9,0E,00,80,BC,00,44,02
  209. 1300 DATA 75,0D,C6,84,00,44,00,FE,85,00
  210. 1310 DATA 44,4F,4E,E2,EC,80,3E,00,44,00
  211. 1320 DATA 75,02,EB,9C,1F,CB,/
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. Program 3: The Snowflake Sweep
  222.  
  223. 10 DIM DX(11),DY(11):KEY OFF
  224. 20 FOR N=0 TO 6:READ SD(N),RD(N):LN(N)=1!/3!:NEXT:LN(2)=SQR(LN(1))
  225. 30 A=0:FOR D=6 TO 11:DX(D)=COS(A):DY(D)=SIN(A)
  226. 40 A=A+.52359879#:NEXT
  227. 50 FOR D=0 TO 5:DX(D)=-DX(D+6):DY(D)=-DY(D+6):NEXT:X1=534:Y1=147:TL=324
  228. 60 CLS:SCREEN 0
  229. 70 PRINT"ENTER NUMBER OF CYCLES (1 - 4)"
  230. 80 INPUT "        OR ENTER A ZERO TO QUIT: ";NC
  231. 90 IF NC=0 THEN END
  232. 100 IF NC>4 THEN 60
  233. 110 CLS:SCREEN 2
  234. 120 X=534:Y=147:TL=324:PSET (X,Y),1
  235. 130 FOR C=0 TO NC:SN(C)=0:NEXT
  236. 140 D=0:L=TL:NS=0:FOR C=1 TO NC:I=SN(C):L=L*LN(I):J=SN(C-1):NS=NS+SD(J):IF NS MOD 2=1 THEN D=D+12-RD(I):GOTO 160
  237. 150 D=D+RD(I)
  238. 160 D=D MOD 12
  239. 170 NEXT
  240. 180 X=X+1.33*L*DX(D):Y=Y-.5*L*DY(D):LINE -(X,Y),1:SN(NC)=SN(NC)+1:FOR C=NC TO 1 STEP -1:IF SN(C)<>7 THEN 200
  241. 190 SN(C)=0:SN(C-1)=SN(C-1)+1:NEXT
  242. 200 IF SN(0)=0 THEN 140
  243. 210 IF INKEY$="" THEN 210
  244. 220 GOTO 60
  245. 230 DATA 0,0,1,0,1,7,0,10,0,0,0,2,1,2
  246.  
  247.